﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrienDev.BusinessRules
{
    public class HtmlHelper
    {
        #region 截取指定长度的字符串
        public static string InterceptSubString(string StringObject, int MaxLength, bool IsAddMore)
        {
            if (StringObject == null)
            {
                StringObject = string.Empty;
            }
            if (StringObject == string.Empty)
            {
                return StringObject;
            }
            if (IsAddMore)
            {
                MaxLength = MaxLength - 3;
                if (MaxLength < 0)
                {
                    MaxLength = 0;
                }
            }
            else
            {
                if (MaxLength < 0)
                {
                    MaxLength = 0;
                }
            }

            int StringObjectLength = 0;
            for (int i = 0; i < StringObject.Length; i++)
            {
                if (Convert.ToInt32(Convert.ToChar(StringObject.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
                {
                    StringObjectLength += 1;
                }
                else
                {
                    StringObjectLength += 2;
                }
                if (StringObjectLength > MaxLength)
                {
                    break;
                }
            }
            if (StringObjectLength > MaxLength)
            {
                int WordNum = 0;
                int digit = 0;
                for (int i = 0; i < StringObject.Length; i++)
                {
                    if (Convert.ToInt32(Convert.ToChar(StringObject.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
                    {
                        digit += 1;
                    }
                    else
                    {
                        digit += 2;
                    }
                    WordNum++;
                    if (digit >= MaxLength)
                    {
                        break;
                    }
                }
                if (digit > MaxLength)
                {
                    WordNum = WordNum - 1;
                    if (WordNum < 0)
                    {
                        WordNum = 0;
                    }
                }
                StringObject = StringObject.Substring(0, WordNum);
                if (IsAddMore)
                {
                    StringObject += "...";
                }

                return StringObject;
            }
            else
            {
                return StringObject;
            }
        }
        #endregion

        public static string HtmlEncode(string strContent)
        {
            strContent = strContent.Replace(">", "&gt;");
            strContent = strContent.Replace("<", "&lt;");
            strContent = strContent.Replace("   ", "&nbsp;");
            strContent = strContent.Replace("\"", "&quot;");
            strContent = strContent.Replace("\n\n", "</P><P>");
            strContent = strContent.Replace("\n", "<br>");
            strContent = strContent.Replace("\r\n", "<BR><BR>");
            return strContent;
        }
    }
}
